package com.netease.qa.web.controller;

import com.netease.qa.biz.dto.TeamDTO;
import com.netease.qa.biz.service.ProjectService;
import com.netease.qa.biz.service.TeamService;
import com.netease.qa.biz.service.TestCaseService;
import com.netease.qa.web.utils.ServiceResult;
import com.netease.qa.web.utils.UserUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author zoudongdong
 * @date 2018/2/15
 */
@Controller
public class TeamController {

    private static Logger log = LoggerFactory.getLogger(TeamController.class);

    @Resource
    private TeamService teamService;

    @RequestMapping(value = "/addTeam", method = RequestMethod.POST)
    @ResponseBody
    public ServiceResult addTeam(@RequestBody TeamDTO team, HttpServletRequest request) {
        log.info("******addTeam******");

        //如果teamId已存在,则添加失败(为了避免用户重复点,出现多个teamId相同团队)
        List<Integer> existTeamIdList = teamService.getTeamIdList();
        if (existTeamIdList.contains(team.getTeamId())) {
            return ServiceResult.successMsg("编号为" + team.getTeamId() + "的团队已经存在,请刷新新增页面!!!");
        }
        //如果teamName已存在,则添加失败
        if (teamService.getTeamByName(team.getTeamName())) {
            return ServiceResult.successMsg("团队名称为" + team.getTeamName() + "的团队已经存在!!!");
        }

        team.setCreator(UserUtils.getCurrentLoginUserId(request));
        teamService.addTeam(team);
        return ServiceResult.successMsg("添加成功");
    }

    @RequestMapping(value = "/getAllTeam", method = RequestMethod.GET)
    @ResponseBody
    public ServiceResult getAllTeam() {
        log.info("******getAllTeam******");

        List<TeamDTO> teamList = teamService.getAllTeam();
        return ServiceResult.success(teamList);
    }

    @RequestMapping(value = "/getJoinedTeamList", method = RequestMethod.GET)
    @ResponseBody
    public ServiceResult getJoinedTeamList(HttpServletRequest request) {
        log.info("******getJoinedTeamList******");

        List<TeamDTO> joinedTeamList = teamService.getJoinedTeamList(UserUtils.getCurrentLoginUserId(request));
        return ServiceResult.success(joinedTeamList);
    }

    @RequestMapping(value = "/getNextTeamId", method = RequestMethod.GET)
    @ResponseBody
    public ServiceResult getNextTeamId() {
        log.info("******getNextTeamId******");

        int nextTeamId = teamService.getNextTeamId();
        return ServiceResult.success(nextTeamId);
    }

    @RequestMapping(value = "/joinTeam", method = RequestMethod.GET)
    @ResponseBody
    public ServiceResult joinTeam(@RequestParam int teamId, HttpServletRequest request) {
        log.info("******joinTeam******");

        String currentUserId = UserUtils.getCurrentLoginUserId(request);
        if (UserUtils.ifUserJoinedTeam(currentUserId, teamId)) {
            return ServiceResult.successMsg("请勿重复加入!!!");
        }
        teamService.joinTeam(currentUserId, teamId);
        return ServiceResult.successMsg("加入成功");
    }

    @RequestMapping(value = "/leaveTeam", method = RequestMethod.GET)
    @ResponseBody
    public ServiceResult leaveTeam(@RequestParam int teamId, HttpServletRequest request) {
        log.info("******leaveTeam******");

        String currentUserId = UserUtils.getCurrentLoginUserId(request);
        //查询该组的所有操作者,判断用户是否已经加入该团队了
        if (UserUtils.ifUserJoinedTeam(currentUserId, teamId)) {
            //如果是该团队的创建者,则不能移出
            TeamDTO team = teamService.getTeamDetail(teamId);
            if (currentUserId.equals(team.getCreator())) {
                return ServiceResult.successMsg("您是该团队的创建者,不能移出!!!");
            }
            teamService.leaveTeam(currentUserId, teamId);
            return ServiceResult.successMsg("移出成功");
        }
        return ServiceResult.successMsg("未加入该团队,不能移出!!!");
    }

    @RequestMapping(value = "/getTeamDetail", method = RequestMethod.GET)
    @ResponseBody
    public ServiceResult getTeamDetail(@RequestParam int teamId) {
        log.info("******getTeamDetail******");

        TeamDTO teamDTO = teamService.getTeamDetail(teamId);
        return ServiceResult.success(teamDTO);
    }

    @RequestMapping(value = "/editTeam", method = RequestMethod.POST)
    @ResponseBody
    public ServiceResult editTeam(@RequestBody TeamDTO team, HttpServletRequest request) {
        log.info("******editTeam******");

        String currentUserId = UserUtils.getCurrentLoginUserId(request);

        //判断当前登录用户是否属于当前组,否则不能修改组信息
        if (!UserUtils.ifUserJoinedTeam(currentUserId, team.getTeamId())) {
            return ServiceResult.successMsg("用户不属于该团队,不能修改团队信息");
        }
        teamService.editTeam(team, currentUserId);
        return ServiceResult.successMsg("修改成功");
    }

    @RequestMapping(value = "/validateBeforeDeleteTeam", method = RequestMethod.GET)
    @ResponseBody
    public ServiceResult validateBeforeDeleteTeam(int teamId) {
        log.info("******validateBeforeDeleteTeam******");
        String msg = teamService.validateBeforeDelete(teamId);
        return ServiceResult.successMsg(msg);
    }

    @RequestMapping(value = "/deleteTeam", method = RequestMethod.POST)
    @ResponseBody
    public ServiceResult deleteTeam(@RequestParam int teamId, HttpServletRequest request) {
        log.info("******deleteTeam******");

        String currentUserId = UserUtils.getCurrentLoginUserId(request);
        //只有团队的creator才有权限删除团队
        TeamDTO teamDTO = teamService.getTeamDetail(teamId);
        if (!currentUserId.equals(teamDTO.getCreator())) {
            return ServiceResult.successMsg("您不是团队的创建者,没有权限删除!!!");
        }
        teamService.deleteTeam(teamId, currentUserId);
        return ServiceResult.successMsg("删除成功");
    }
}
